package com.work.keep.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.work.keep.R;
import com.work.keep.activity.BigPhotoActivity;
import com.work.keep.datas.FragmentOtherDynamicData;
import com.work.keep.datas.PeopleData;
import com.work.keep.volley.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/19.
 */
public class MyOtherUserRecyclerAdapter extends RecyclerView.Adapter<MyOtherUserRecyclerAdapter.OtherPhotoViewHolder> {
    private Context context;
    private List<FragmentOtherDynamicData.DataEntity> photoLists;
    private VolleySingleton volleySingleton;
    private ImageLoader imageLoader;


    public MyOtherUserRecyclerAdapter(Context context) {
        this.context = context;
        init();
    }

    public void addData(List<FragmentOtherDynamicData.DataEntity> photoLists) {
        this.photoLists.addAll(photoLists);
        notifyDataSetChanged();
    }

    private void init() {
        this.photoLists = new ArrayList<>();
        volleySingleton = VolleySingleton.getInstance();
        imageLoader = volleySingleton._getImageLoader();
    }

    @Override
    public OtherPhotoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.fragment_people_photo_recycler_item, parent, false);
        OtherPhotoViewHolder otherPhotoViewHolder = new OtherPhotoViewHolder(v);
        return otherPhotoViewHolder;
    }

    @Override
    public void onBindViewHolder(OtherPhotoViewHolder holder, final int position) {
        holder.fragmentPhotoRecyclerNI.setDefaultImageResId(R.mipmap.ic_launcher);
        holder.fragmentPhotoRecyclerNI.setErrorImageResId(R.mipmap.ic_launcher);
        holder.fragmentPhotoRecyclerNI.setImageUrl(photoLists.get(position).getPhoto(), imageLoader);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BigPhotoActivity.goToBigPhotoActivity(context,photoLists, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return photoLists.size();
    }

    class OtherPhotoViewHolder extends RecyclerView.ViewHolder {
        private NetworkImageView fragmentPhotoRecyclerNI;

        public OtherPhotoViewHolder(View itemView) {
            super(itemView);
            fragmentPhotoRecyclerNI = (NetworkImageView) itemView.findViewById(R.id.fragmentPhotoRecyclerNI);
        }
    }
}
